package com.plugin.seed.mybatis.defaultvalue.processor;

import com.plugin.seed.mybatis.defaultvalue.util.DefaultValueUtil;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * @author Peter Fan
 */
@Slf4j
public class SeedMybatisBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof MybatisProperties) {
            MybatisProperties properties = (MybatisProperties) bean;
            if (ObjectUtils.isEmpty(properties.getMapperLocations())) {
                properties.setMapperLocations(new String[]{"classpath:mapper/*.xml"});
            }

            if (StringUtils.hasLength(properties.getTypeAliasesPackage())) {
                DefaultValueUtil.flushFieldDefaultValueMap(properties.getTypeAliasesPackage());
            }

        }
        return bean;
    }
}
